﻿using System;
using System.Text;
using System.Text.RegularExpressions;

namespace VSSDKAssist.Assets.Helpers.KeyRegistration
{
	public sealed class SLKRegistrator : Registrator
	{
		public SLKRegistrator(
			string companyName,
			string shellName,
			Guid shellGuid,
			string shellVersion)
			: base(companyName, shellName, shellGuid, shellVersion)
		{
		}

		protected override string GetKey(string aspxPage)
		{
			string response = string.Empty;

			Regex reg = new Regex(
				".*Shell Load Key \\(SLK\\)</b></td><td>(?<SLK>.*)</td></tr>.*",
				RegexOptions.Compiled | RegexOptions.IgnoreCase);

			if(reg.IsMatch(aspxPage))
			{
				response = reg.Match(aspxPage).Groups["SLK"].Value;
				response = response.Replace("<br>", string.Empty).Trim();
			}

			return response;
		}

		protected override string GetPostData(string aspxPage)
		{
			StringBuilder postData = new StringBuilder();

			postData.Append(base.GetPostData(aspxPage));

			postData.Append(string.Format("__EVENTTARGET={0}&", GetDoPostBackName(aspxPage, "btnShellSave")));
			postData.Append(string.Format("{0}={1}&", GetInputName(aspxPage, "txtShellCompany"), this.CompanyName));
			postData.Append(string.Format("{0}={1}&", GetInputName(aspxPage, "txtShellName"), this.Name));
			postData.Append(string.Format("{0}={1}&", GetInputName(aspxPage, "txtShellGuid"), this.Guid.ToString("D")));
			postData.Append(string.Format("{0}={1}&", GetInputName(aspxPage, "txtShellVersion"), this.Version));

			return postData.ToString();
		}
	}
}